﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml.Linq;
using System.Windows.Forms;
using System.Threading;

namespace Reticle
{
    public static class Manager
    {
        public static List<Bot> Bots { get; private set; }
        public static string TabKey { get; set; }
        public static XDocument ServerXML { get; private set; }

        static Manager()
        {
            Bots = new List<Bot>();
        }

        public static Bot CreateBot(Settings settings, string key, string homechannel)
        {
            Bot bot = new Bot(Bots.Count, settings, key, homechannel);
            Bots.Add(bot);
            return bot;
        }

        public static Dictionary<string, string> LoadSettings()
        {
            Dictionary<string, string> d = new Dictionary<string, string>();
            XDocument doc = XDocument.Load(Application.StartupPath + @"\files\config.xml");

            foreach (XElement xe in doc.Descendants("Settings"))
            {
                d.Add("chatevent", xe.Element("chatevent").Value);
                d.Add("urllinks", xe.Element("urllinks").Value);
                d.Add("imagebot", xe.Element("imagebot").Value);
                d.Add("youtubebot", xe.Element("youtubebot").Value);
                d.Add("youtubeembed", xe.Element("youtubeembed").Value);
                d.Add("imageembed", xe.Element("imageembed").Value);
                d.Add("notifytalk", xe.Element("notifytalk").Value);
                d.Add("notifyemote", xe.Element("notifyemote").Value);
                d.Add("notifywhisper", xe.Element("notifywhisper").Value);
            }

            foreach (XElement xe2 in doc.Descendants("Verbytes"))
            {
                d.Add("star-verbyte", xe2.Element("star").Value);
                d.Add("w2bn-verbyte", xe2.Element("w2bn").Value);
                d.Add("d2dv-verbyte", xe2.Element("d2dv").Value);
                d.Add("war3-verbyte", xe2.Element("war3").Value);
            }

            return d;
        }

        public static Int32 GetVerbyte(string client)
        {
            XDocument doc = XDocument.Load(Application.StartupPath + @"\files\config.xml");
            Int32 verbyte = 0x00;

            foreach (XElement xe in doc.Descendants("Verbytes"))
            {
                switch (client)
                {
                    case "STAR":
                    case "SEXP":
                        verbyte = Convert.ToInt32("0x" + xe.Element("star").Value, 16);
                        break;
                    case "W2BN":
                        verbyte = Convert.ToInt32("0x" + xe.Element("w2bn").Value, 16);
                        break;
                    case "D2DV":
                    case "D2XP":
                        verbyte = Convert.ToInt32("0x" + xe.Element("d2dv").Value, 16);
                        break;
                    case "WAR3":
                    case "W3XP":
                        verbyte = Convert.ToInt32("0x" + xe.Element("war3").Value, 16);
                        break;
                }
            }

            return verbyte;
        }

        // get the info.xml from the server (holds verbyte info, plugins, etc)
        public static void FetchServerXML()
        {
            ServerXML = XDocument.Load("http://www.bnetweb.org/reticle/info.xml");
        }

        // Get value of ServerXML (the XDocument that is)
        public static XDocument GetServerXML()
        {
            return ServerXML;
        }

        public static string FetchNews()
        {
            XDocument doc = XDocument.Load("http://www.bnetweb.org/reticle/info.xml");

            foreach (XElement xe in doc.Descendants("Bot"))
            {
                return xe.Element("news").Value;
            }

            return "There was a problem connecting to the news server, please try again later.";
        }

        public static void FetchVerbytes()
        {
            XDocument doc = XDocument.Load("http://www.bnetweb.org/reticle/info.xml");

            bool star = false, w2bn = false, d2dv = false, war3 = false;

            // reload bot verbytes (all settings)
            Program.Window.RefreshSettings();
            foreach (XElement xe in doc.Descendants("Verbytes"))
            {
                // check star/sexp
                if (xe.Element("star").Value == Program.Window.checkSetting("star-verbyte"))
                {
                    star = true;
                }
                else
                {
                    string msg = saveVerbyte("STAR", xe.Element("star").Value);

                    Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">{1}</span></div>", Helper.TimeStamp(), msg));
                    Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">{1}</span></div>", Helper.TimeStamp(), msg));

                    star = true;
                }

                // check w2bn
                if (xe.Element("w2bn").Value == Program.Window.checkSetting("w2bn-verbyte"))
                {
                    w2bn = true;
                }
                else
                {
                    string msg = saveVerbyte("W2BN", xe.Element("w2bn").Value);

                    Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">{1}</span></div>", Helper.TimeStamp(), msg));
                    Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">{1}</span></div>", Helper.TimeStamp(), msg));

                    w2bn = true;
                }

                // check d2dv/d2xp
                if (xe.Element("d2dv").Value == Program.Window.checkSetting("d2dv-verbyte"))
                {
                    d2dv = true;
                }
                else
                {
                    string msg = saveVerbyte("D2DV", xe.Element("d2dv").Value);

                    Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">{1}</span></div>", Helper.TimeStamp(), msg));
                    Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">{1}</span></div>", Helper.TimeStamp(), msg));

                    d2dv = true;
                }

                // check war3/w3xp
                if (xe.Element("war3").Value == Program.Window.checkSetting("war3-verbyte"))
                {
                    war3 = true;
                }
                else
                {
                    string msg = saveVerbyte("WAR3", xe.Element("war3").Value);

                    Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">{1}</span></div>", Helper.TimeStamp(), msg));
                    Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">{1}</span></div>", Helper.TimeStamp(), msg));

                    war3 = true;
                }
            }

            // All verbytes are up to date (according to server)
            if (star && w2bn && d2dv && war3)
            {
                Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">All verbytes are up to date.</span></div>", Helper.TimeStamp()));
                Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">All verbytes are up to date.</span></div>", Helper.TimeStamp()));
            }
        }

        public static string saveVerbyte(string product, string verbyte)
        {
            XDocument doc = XDocument.Load(Application.StartupPath + @"\files\config.xml");
            string returnText = "";

            switch(product)
            {
                case "STAR":
                    doc.Element("Config").Element("Verbytes").Element("star").Value = verbyte;
                    returnText = String.Format("Starcraft has been updated to {0}.", verbyte);
                    break;
                case "W2BN":
                    doc.Element("Config").Element("Verbytes").Element("w2bn").Value = verbyte;
                    returnText = String.Format("Warcraft II: BNE has been updated to {0}.", verbyte);
                    break;
                case "D2DV":
                    doc.Element("Config").Element("Verbytes").Element("d2dv").Value = verbyte;
                    returnText = String.Format("Diablo has been updated to {0}.", verbyte);
                    break;
                case "WAR3":
                    doc.Element("Config").Element("Verbytes").Element("war3").Value = verbyte;
                    returnText = String.Format("Warcraft III has been updated to {0}.", verbyte);
                    break;
            }

            doc.Save(Application.StartupPath + @"\files\config.xml");
            return returnText;
        }

        public static string CheckVersion()
        {
            XDocument doc = XDocument.Load("http://www.bnetweb.org/reticle/info.xml");
            //XDocument doc = Manager.GetServerXML();

            string version = "";

            foreach (XElement xe in doc.Descendants("Bot"))
            {
                version = xe.Element("version").Value;
            }

            if (version == "")
            {
                return "error";
            }

            if (version == Program.Window.GetType().Assembly.GetName().Version.ToString())
            {
                return "current";
            }
            else
            {
                return "outdated";
            }
        }

        public static void LoadProfileSettings(string botKey)
        {
            XDocument doc = XDocument.Load(Application.StartupPath + @"\files\config.xml");

            // get/set per profile settings
            foreach (Bot bot in Bots)
            {
                if (botKey == bot.BotKey)
                {
                    foreach (XElement xe in doc.Descendants("Profile"))
                    {
                        if (xe.Element("id").Value == botKey)
                        {
                            if (xe.Element("showjoinleave").Value == "1")
                            {
                                bot.ShowJoinLeave = true;
                            }
                            else
                            {
                                bot.ShowJoinLeave = false;
                            }

                            if (xe.Element("disablequeue").Value == "1")
                            {
                                bot.DisableQueue = true;
                            }
                            else
                            {
                                bot.DisableQueue = false;
                            }
                        }
                    }
                }
            }
        }

        public static void SaveProfileSettings(string botKey)
        {
            XDocument doc = XDocument.Load(Application.StartupPath + @"\files\config.xml");

            // get/set per profile settings
            foreach (Bot bot in Bots)
            {
                if (botKey == bot.BotKey)
                {
                    foreach (XElement xe in doc.Descendants("Profile"))
                    {
                        if (xe.Element("id").Value == botKey)
                        {
                            if (bot.ShowJoinLeave)
                            {
                                xe.Element("showjoinleave").Value = "1";
                            }
                            else
                            {
                                xe.Element("showjoinleave").Value = "0";
                            }

                            if (bot.DisableQueue)
                            {
                                xe.Element("disablequeue").Value = "1";
                            }
                            else
                            {
                                xe.Element("disablequeue").Value = "0";
                            }
                        }
                    }
                }
            }

            doc.Save(Application.StartupPath + @"\files\config.xml");
        }

        public static string CheckHashes(string product, string file)
        {
            switch (product)
            {
                case "STAR":
                case "SEXP":
                    switch (file)
                    {
                        case "GameExe":
                            if (File.Exists(Application.StartupPath + @"\files\hashes\STAR\Starcraft.exe"))
                            {
                                return Application.StartupPath + @"\files\hashes\STAR\Starcraft.exe";
                            }
                            else
                            {
                                return Application.StartupPath + @"\files\config.xml";
                            }
                        case "GameFile2":
                            if (File.Exists(Application.StartupPath + @"\files\hashes\STAR\Storm.dll"))
                            {
                                return Application.StartupPath + @"\files\hashes\STAR\Storm.dll";
                            }
                            else
                            {
                                return Application.StartupPath + @"\files\config.xml";
                            }
                        case "GameFile3":
                            if (File.Exists(Application.StartupPath + @"\files\hashes\STAR\Battle.snp"))
                            {
                                return Application.StartupPath + @"\files\hashes\STAR\Battle.snp";
                            }
                            else
                            {
                                return Application.StartupPath + @"\files\config.xml";
                            }
                        case "ImageFile":
                            if (File.Exists(Application.StartupPath + @"\files\hashes\bin\STAR.bin"))
                            {
                                return Application.StartupPath + @"\files\hashes\bin\STAR.bin";
                            }
                            else
                            {
                                return Application.StartupPath + @"\files\config.xml";
                            }
                    }
                    break;
                case "W2BN":
                    switch (file)
                    {
                        case "GameExe":
                            if (File.Exists(Application.StartupPath + @"\files\hashes\W2BN\Warcraft II BNE.exe"))
                            {
                                return Application.StartupPath + @"\files\hashes\W2BN\Warcraft II BNE.exe";
                            }
                            else
                            {
                                return Application.StartupPath + @"\files\config.xml";
                            }
                        case "GameFile2":
                            if (File.Exists(Application.StartupPath + @"\files\hashes\W2BN\Storm.dll"))
                            {
                                return Application.StartupPath + @"\files\hashes\W2BN\Storm.dll";
                            }
                            else
                            {
                                return Application.StartupPath + @"\files\config.xml";
                            }
                        case "GameFile3":
                            if (File.Exists(Application.StartupPath + @"\files\hashes\W2BN\Battle.snp"))
                            {
                                return Application.StartupPath + @"\files\hashes\W2BN\Battle.snp";
                            }
                            else
                            {
                                return Application.StartupPath + @"\files\config.xml";
                            }
                        case "ImageFile":
                            if (File.Exists(Application.StartupPath + @"\files\hashes\bin\W2BN.bin"))
                            {
                                return Application.StartupPath + @"\files\hashes\bin\W2BN.bin";
                            }
                            else
                            {
                                return Application.StartupPath + @"\files\config.xml";
                            }
                    }
                    break;
                case "D2DV":
                    switch (file)
                    {
                        case "GameExe":
                            if (File.Exists(Application.StartupPath + @"\files\hashes\D2DV\Game.exe"))
                            {
                                return Application.StartupPath + @"\files\hashes\D2DV\Game.exe";
                            }
                            else
                            {
                                return Application.StartupPath + @"\files\config.xml";
                            }
                        case "GameFile2":
                            if (File.Exists(Application.StartupPath + @"\files\hashes\D2DV\Bnclient.dll"))
                            {
                                return Application.StartupPath + @"\files\hashes\D2DV\Bnclient.dll";
                            }
                            else
                            {
                                return Application.StartupPath + @"\files\config.xml";
                            }
                        case "GameFile3":
                            if (File.Exists(Application.StartupPath + @"\files\hashes\D2DV\D2Client.dll"))
                            {
                                return Application.StartupPath + @"\files\hashes\D2DV\D2Client.dll";
                            }
                            else
                            {
                                return Application.StartupPath + @"\files\config.xml";
                            }
                        case "ImageFile":
                            if (File.Exists(Application.StartupPath + @"\files\hashes\bin\D2DV.bin"))
                            {
                                return Application.StartupPath + @"\files\hashes\bin\D2DV.bin";
                            }
                            else
                            {
                                return Application.StartupPath + @"\files\config.xml";
                            }
                    }
                    break;
                case "D2XP":
                    switch (file)
                    {
                        case "GameExe":
                            if (File.Exists(Application.StartupPath + @"\files\hashes\D2XP\Game.exe"))
                            {
                                return Application.StartupPath + @"\files\hashes\D2XP\Game.exe";
                            }
                            else
                            {
                                return Application.StartupPath + @"\files\config.xml";
                            }
                        case "GameFile2":
                            if (File.Exists(Application.StartupPath + @"\files\hashes\D2XP\Bnclient.dll"))
                            {
                                return Application.StartupPath + @"\files\hashes\D2XP\Bnclient.dll";
                            }
                            else
                            {
                                return Application.StartupPath + @"\files\config.xml";
                            }
                        case "GameFile3":
                            if (File.Exists(Application.StartupPath + @"\files\hashes\D2XP\D2Client.dll"))
                            {
                                return Application.StartupPath + @"\files\hashes\D2XP\D2Client.dll";
                            }
                            else
                            {
                                return Application.StartupPath + @"\files\config.xml";
                            }
                        case "ImageFile":
                            if (File.Exists(Application.StartupPath + @"\files\hashes\bin\D2XP.bin"))
                            {
                                return Application.StartupPath + @"\files\hashes\bin\D2XP.bin";
                            }
                            else
                            {
                                return Application.StartupPath + @"\files\config.xml";
                            }
                    }
                    break;
                case "WAR3":
                case "W3XP":
                    switch (file)
                    {
                        case "GameExe":
                            if (File.Exists(Application.StartupPath + @"\files\hashes\WAR3\war3.exe"))
                            {
                                return Application.StartupPath + @"\files\hashes\WAR3\war3.exe";
                            }
                            else
                            {
                                return Application.StartupPath + @"\files\config.xml";
                            }
                        case "GameFile2":
                            if (File.Exists(Application.StartupPath + @"\files\hashes\WAR3\Storm.dll"))
                            {
                                return Application.StartupPath + @"\files\hashes\WAR3\Storm.dll";
                            }
                            else
                            {
                                return Application.StartupPath + @"\files\config.xml";
                            }
                        case "GameFile3":
                            if (File.Exists(Application.StartupPath + @"\files\hashes\WAR3\game.dll"))
                            {
                                return Application.StartupPath + @"\files\hashes\WAR3\game.dll";
                            }
                            else
                            {
                                return Application.StartupPath + @"\files\config.xml";
                            }
                        case "ImageFile":
                            if (File.Exists(Application.StartupPath + @"\files\hashes\bin\WAR3.bin"))
                            {
                                return Application.StartupPath + @"\files\hashes\bin\WAR3.bin";
                            }
                            else
                            {
                                return Application.StartupPath + @"\files\config.xml";
                            }
                    }
                    break;
            }

            return null;
        }

        // check for hashes when a user selects a product
        public static bool CheckConfigHashes(string product)
        {
            switch (product)
            {
                case "STAR":
                case "SEXP":
                    if (File.Exists(Application.StartupPath + @"\files\hashes\STAR\Starcraft.exe") && File.Exists(Application.StartupPath + @"\files\hashes\STAR\Storm.dll") && File.Exists(Application.StartupPath + @"\files\hashes\STAR\Battle.snp"))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case "W2BN":
                    if (File.Exists(Application.StartupPath + @"\files\hashes\W2BN\Warcraft II BNE.exe") && File.Exists(Application.StartupPath + @"\files\hashes\W2BN\Storm.dll") && File.Exists(Application.StartupPath + @"\files\hashes\W2BN\Battle.snp"))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case "D2DV":
                    if (File.Exists(Application.StartupPath + @"\files\hashes\D2DV\Game.exe") && File.Exists(Application.StartupPath + @"\files\hashes\D2DV\Bnclient.dll") && File.Exists(Application.StartupPath + @"\files\hashes\D2DV\D2Client.dll"))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case "D2XP":
                    if (File.Exists(Application.StartupPath + @"\files\hashes\D2XP\Game.exe") && File.Exists(Application.StartupPath + @"\files\hashes\D2XP\Bnclient.dll") && File.Exists(Application.StartupPath + @"\files\hashes\D2XP\D2Client.dll"))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                case "WAR3":
                case "W3XP":
                    if (File.Exists(Application.StartupPath + @"\files\hashes\WAR3\war3.exe") && File.Exists(Application.StartupPath + @"\files\hashes\WAR3\Storm.dll") && File.Exists(Application.StartupPath + @"\files\hashes\WAR3\game.dll"))
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
            }

            return false;
        }

        public static string GetClientIcon(int wins, string client)
        {
            switch (client.ToLower())
            {
                case "star":
                    if (wins <= 0)
                    {
                        return "star-0";
                    }
                    else if (wins == 1)
                    {
                        return "star-1";
                    }
                    else if (wins == 2)
                    {
                        return "star-2";
                    }
                    else if (wins == 3)
                    {
                        return "star-3";
                    }
                    else if (wins == 4)
                    {
                        return "star-4";
                    }
                    else if (wins == 5)
                    {
                        return "star-5";
                    }
                    else if (wins == 6)
                    {
                        return "star-6";
                    }
                    else if (wins == 7)
                    {
                        return "star-7";
                    }
                    else if (wins == 8)
                    {
                        return "star-8";
                    }
                    else if (wins == 9)
                    {
                        return "star-9";
                    }
                    else if (wins >= 10)
                    {
                        return "star-10";
                    }
                    else
                    {
                        return "star";
                    }
                case "sexp":
                    if (wins <= 0)
                    {
                        return "star-0";
                    }
                    else if (wins == 1)
                    {
                        return "star-1";
                    }
                    else if (wins == 2)
                    {
                        return "star-2";
                    }
                    else if (wins == 3)
                    {
                        return "star-3";
                    }
                    else if (wins == 4)
                    {
                        return "star-4";
                    }
                    else if (wins == 5)
                    {
                        return "star-5";
                    }
                    else if (wins == 6)
                    {
                        return "star-6";
                    }
                    else if (wins == 7)
                    {
                        return "star-7";
                    }
                    else if (wins == 8)
                    {
                        return "star-8";
                    }
                    else if (wins == 9)
                    {
                        return "star-9";
                    }
                    else if (wins >= 10)
                    {
                        return "star-10";
                    }
                    else
                    {
                        return "sexp";
                    }
                case "w2bn":
                    if (wins <= 0)
                    {
                        return "w2bn-0";
                    }
                    else if (wins == 1)
                    {
                        return "w2bn-1";
                    }
                    else if (wins == 2)
                    {
                        return "w2bn-2";
                    }
                    else if (wins == 3)
                    {
                        return "w2bn-3";
                    }
                    else if (wins == 4)
                    {
                        return "w2bn-4";
                    }
                    else if (wins == 5)
                    {
                        return "w2bn-5";
                    }
                    else if (wins == 6)
                    {
                        return "w2bn-6";
                    }
                    else if (wins == 7)
                    {
                        return "w2bn-7";
                    }
                    else if (wins == 8)
                    {
                        return "w2bn-8";
                    }
                    else if (wins == 9)
                    {
                        return "w2bn-9";
                    }
                    else if (wins >= 10)
                    {
                        return "w2bn-10";
                    }
                    else
                    {
                        return "w2bn";
                    }
                default:
                    return null;
            }
        }

        public static void DownloadHashes(object product)
        {
            WebClient webClient = new WebClient();

            if ((string)product != "")
            {
                switch ((string)product.ToString().ToUpper())
                {
                    case "STAR":
                    case "SEXP":
                        webClient.DownloadFile(new Uri("http://www.bnetweb.org/reticle/hashes/star/battle.snp"), Application.StartupPath + @"\files\hashes\STAR\Battle.snp");

                        Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded Battle.snp</span></div>", Helper.TimeStamp()));
                        Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded Battle.snp</span></div>", Helper.TimeStamp()));

                        webClient.DownloadFile(new Uri("http://www.bnetweb.org/reticle/hashes/star/starcraft.exe"), Application.StartupPath + @"\files\hashes\STAR\StarCraft.exe");

                        Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded StarCraft.exe</span></div>", Helper.TimeStamp()));
                        Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded StarCraft.exe</span></div>", Helper.TimeStamp()));
                        
                        webClient.DownloadFile(new Uri("http://www.bnetweb.org/reticle/hashes/star/storm.dll"), Application.StartupPath + @"\files\hashes\STAR\Storm.dll");

                        Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded Storm.dll</span></div>", Helper.TimeStamp()));
                        Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded Storm.snp</span></div>", Helper.TimeStamp()));

                        Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Hashes downloaded successfully! Try to connect again.</span></div>", Helper.TimeStamp()));
                        Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Hashes downloaded successfully! Try to connect again.</span></div>", Helper.TimeStamp()));
                        break;
                    case "W2BN":
                        webClient.DownloadFile(new Uri("http://www.bnetweb.org/reticle/hashes/w2bn/battle.snp"), Application.StartupPath + @"\files\hashes\W2BN\Battle.snp");
                        
                        Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded Battle.snp</span></div>", Helper.TimeStamp()));
                        Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded Battle.snp</span></div>", Helper.TimeStamp()));
                        
                        webClient.DownloadFile(new Uri("http://www.bnetweb.org/reticle/hashes/w2bn/warcraftiibne.exe"), Application.StartupPath + @"\files\hashes\W2BN\Warcraft II BNE.exe");
                        
                        Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded Warcraft II BNE.exe</span></div>", Helper.TimeStamp()));
                        Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded Warcraft II BNE.exe</span></div>", Helper.TimeStamp()));
                        
                        webClient.DownloadFile(new Uri("http://www.bnetweb.org/reticle/hashes/w2bn/storm.dll"), Application.StartupPath + @"\files\hashes\W2BN\Storm.dll");
                        
                        Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded Storm.dll</span></div>", Helper.TimeStamp()));
                        Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded Storm.dll</span></div>", Helper.TimeStamp()));
                        
                        Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Hashes downloaded successfully! Try to connect again.</span></div>", Helper.TimeStamp()));
                        Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Hashes downloaded successfully! Try to connect again.</span></div>", Helper.TimeStamp()));
                        break;
                    case "D2DV":
                        webClient.DownloadFile(new Uri("http://www.bnetweb.org/reticle/hashes/d2dv/bnclient.dll"), Application.StartupPath + @"\files\hashes\D2DV\Bnclient.dll");

                        Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded Bnclient.dll</span></div>", Helper.TimeStamp()));
                        Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded Bnclient.dll</span></div>", Helper.TimeStamp()));

                        webClient.DownloadFile(new Uri("http://www.bnetweb.org/reticle/hashes/d2dv/game.exe"), Application.StartupPath + @"\files\hashes\D2DV\Game.exe");

                        Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded Game.exe</span></div>", Helper.TimeStamp()));
                        Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded Game.exe</span></div>", Helper.TimeStamp()));

                        webClient.DownloadFile(new Uri("http://www.bnetweb.org/reticle/hashes/d2dv/d2client.dll"), Application.StartupPath + @"\files\hashes\D2DV\D2Client.dll");

                        Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded D2Client.dll</span></div>", Helper.TimeStamp()));
                        Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded D2Client.dll</span></div>", Helper.TimeStamp()));

                        Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Hashes downloaded successfully! Try to connect again.</span></div>", Helper.TimeStamp()));
                        Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Hashes downloaded successfully! Try to connect again.</span></div>", Helper.TimeStamp()));
                        break;
                    case "D2XP":
                        webClient.DownloadFile(new Uri("http://www.bnetweb.org/reticle/hashes/d2xp/bnclient.dll"), Application.StartupPath + @"\files\hashes\D2XP\Bnclient.dll");

                        Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded Bnclient.dll</span></div>", Helper.TimeStamp()));
                        Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded Bnclient.dll</span></div>", Helper.TimeStamp()));

                        webClient.DownloadFile(new Uri("http://www.bnetweb.org/reticle/hashes/d2xp/game.exe"), Application.StartupPath + @"\files\hashes\D2XP\Game.exe");

                        Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded Game.exe</span></div>", Helper.TimeStamp()));
                        Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded Game.exe</span></div>", Helper.TimeStamp()));

                        webClient.DownloadFile(new Uri("http://www.bnetweb.org/reticle/hashes/d2xp/d2client.dll"), Application.StartupPath + @"\files\hashes\D2XP\D2Client.dll");

                        Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded D2Client.dll</span></div>", Helper.TimeStamp()));
                        Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded D2Client.dll</span></div>", Helper.TimeStamp()));

                        Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Hashes downloaded successfully! Try to connect again.</span></div>", Helper.TimeStamp()));
                        Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Hashes downloaded successfully! Try to connect again.</span></div>", Helper.TimeStamp()));
                        break;
                    case "WAR3":
                    case "W3XP":
                        webClient.DownloadFile(new Uri("http://www.bnetweb.org/reticle/hashes/war3/game.dll"), Application.StartupPath + @"\files\hashes\WAR3\game.dll");

                        Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded game.dll</span></div>", Helper.TimeStamp()));
                        Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded game.dll</span></div>", Helper.TimeStamp()));

                        webClient.DownloadFile(new Uri("http://www.bnetweb.org/reticle/hashes/war3/war3.exe"), Application.StartupPath + @"\files\hashes\WAR3\war3.exe");

                        Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded war3.exe</span></div>", Helper.TimeStamp()));
                        Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded war3.exe</span></div>", Helper.TimeStamp()));

                        webClient.DownloadFile(new Uri("http://www.bnetweb.org/reticle/hashes/war3/storm.dll"), Application.StartupPath + @"\files\hashes\WAR3\Storm.dll");

                        Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded Storm.dll</span></div>", Helper.TimeStamp()));
                        Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Downloaded Storm.dll</span></div>", Helper.TimeStamp()));

                        Program.Window.InvokeScript(Program.Window.CurrentTabKey(), Program.Window.checkSetting("chatevent"), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Hashes downloaded successfully! Try to connect again.</span></div>", Helper.TimeStamp()));
                        Program.Window.AddChat(Program.Window.CurrentTabKey(), String.Format("<div id=\"chattext\"><span id=\"timestamp\">{0}</span><span class=\"green\">Hashes downloaded successfully! Try to connect again.</span></div>", Helper.TimeStamp()));
                        break;
                }
            }
        }

        public static XDocument GetDocument(string source)
        {
            XDocument returnedData;
            returnedData = XDocument.Load(source);
            TimeSpan timeout = TimeSpan.FromSeconds(10);
            Thread t = new Thread(
                         delegate()
                         {
                             // Return value directly to local variable.
                             returnedData = XDocument.Load(source);
                         });
            t.Start();
            bool success = t.Join(timeout); // Wait for the thread until the timeout expires.
            if (success)
                return returnedData;
            else
                throw new TimeoutException();
        }

        public static string GrabURL(string url)
        {
            try
            {
                string strResult;
                HttpWebRequest reqFirstPage = (HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse resURL = (HttpWebResponse)reqFirstPage.GetResponse();
                StreamReader srURL = new StreamReader(resURL.GetResponseStream());
                strResult = srURL.ReadToEnd();

                return strResult;
            }
            catch (Exception)
            {
                return "";
            }
        }

        public static string GetClanRealm(string realm)
        {
            switch (realm)
            {
                case "Azeroth":
                    return "classic";
                case "Lordaeron":
                    return "classic";
                case "Northrend":
                    return "classic";
                case "Kalimdor":
                    return "asialadders";
                default:
                    return "classic";
            }
        }

        public static string ParseClanName(string data)
        {
            if (data.Contains("<title>Warcraft III Ladder</title>") || data.Contains("<title>Frozen Throne Ladder</title>"))
            {
                return "";
            }

            try
            {
                string temp;
                int location;

                location = data.IndexOf("CLAN STUFF");
                temp = data.Substring(location, data.Length - location);
                location = temp.IndexOf("<span") + 6;
                temp = temp.Substring(location, temp.Length - location);
                location = temp.IndexOf(">") + 2;
                temp = temp.Substring(location - 1, temp.Length - location);
                location = temp.IndexOf(" [");
                temp = temp.Substring(0, location);
                
                return temp;
            }
            catch (Exception)
            {
                return "";
            }
        }

        public static string ParseClanChief(string data)
        {
            if (data.Contains("<title>Warcraft III Ladder</title>") || data.Contains("<title>Frozen Throne Ladder</title>"))
            {
                return "";
            }

            try
            {
                string temp;
                int location;

                location = data.IndexOf("rank4") + 5;
                temp = data.Substring(location, data.Length - location);
                location = temp.IndexOf("PlayerName=") + 11;
                temp = temp.Substring(location, temp.Length - location);
                location = temp.IndexOf(">") + 2;
                temp = temp.Substring(location - 1, temp.Length - location);
                location = temp.IndexOf("</a");
                temp = temp.Substring(0, location);

                return temp;
            }
            catch (Exception)
            {
                return "";
            }
        }

        public static string ParseClanCreated(string data)
        {
            if (data.Contains("<title>Warcraft III Ladder</title>") || data.Contains("<title>Frozen Throne Ladder</title>"))
            {
                return "";
            }

            try
            {
                string temp;
                int location;

                location = data.IndexOf("Creation Date: ") + 15;
                temp = data.Substring(location, data.Length - location);
                location = temp.IndexOf(">") + 1;
                temp = temp.Substring(location, temp.Length - location);
                location = temp.IndexOf("<") - 1;
                temp = temp.Substring(0, location);

                return temp;
            }
            catch (Exception)
            {
                return "";
            }
        }

        public static string HttpPost(string URI, string Parameters)
        {
            System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
            //req.Proxy = new System.Net.WebProxy(ProxyString, true);

            req.ContentType = "application/x-www-form-urlencoded";
            req.Method = "POST";

            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
            req.ContentLength = bytes.Length;

            System.IO.Stream os = req.GetRequestStream();
            os.Write(bytes, 0, bytes.Length);
            os.Close();

            System.Net.WebResponse resp = req.GetResponse();
            if (resp == null) return null;

            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
            return sr.ReadToEnd().Trim();
        }
    }
}
